package com.easthome.job;

import com.easthome.constant.RedisConstant;
import io.minio.DeleteObjectTagsArgs;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定时执行的任务
 */
public class ClearImg {
    @Autowired
    private JedisPool jedisPool;


    /**
     * 定时器组件 ，定时调用
     */
    public void clearImg(){
        try{
        MinioClient client =
                MinioClient.builder().endpoint("http://127.0.0.1:9000")
                        .credentials("minioadmin","minioadmin")
                        .build();
        Jedis jedis = jedisPool.getResource();
        //取出Redis中差集数据
       Set<String> set =
               jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,RedisConstant.SETMEAL_PIC_DB_RESOURCES);
       for (String fileName : set) {
           RemoveObjectArgs remove =  RemoveObjectArgs.builder()
                   .bucket("picture")
                   .object(fileName)
                   .build();
           //方法删除文件
           client.removeObject(remove);
           System.out.println("删除文件成功");
       }
       //删除Redis中的键
       jedis.del(RedisConstant.SETMEAL_PIC_RESOURCES,RedisConstant.SETMEAL_PIC_DB_RESOURCES);
       client.close();
       }catch (Exception ex){
            ex.printStackTrace(); 
        }
    }
}
